var basePage = require('../../../common/BasePage');
var util = require('../../../utils/util');
Page({
  behaviors: [basePage],
  data: {
    /**订单数据 */
    pdc: {},
    /**评论内容 */
    content: '',
    /**投诉数量 */
    refund_num: '',
    /**投诉金额*/
    refund_amount: '',
    img_fileList: [],
    video_fileList:[],
    /** 售后提示*/
    tip:"*发货24小时内到货售后100%，发货24～36小时内到货售后50%，发货36～48小时内到货售后30%，48小时以上无售后。"
  },
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    let that = this;
    eventChannel.on('pdcData', function (data) {
      console.log("要评价的产品数据", data);
      if(data.wh_id!=1){
        that.data.tip="下单配送到店请当面确认数量、质量点击确认收货，有问题当面申请售后，过时不予售后";
      }
      that.setData({
        pdc: data,
        tip:that.data.tip
      })
    })
  },
  Image_afterRead(event) {
    let that = this;
    const file = event.detail.file;
    let img_fileList = this.data.img_fileList;
    // console.log("file",file);
    img_fileList.push(file);
    file.status = "uploading";
    this.setData({
      img_fileList
    });
    wx.uploadFile({
      filePath: file.url,
      name: 'image',
      url: that.data.host + '/home/upload/image',
      formData: {
        from: 'comment'
      },
      success(res) {

        file.status = "failed";
        console.log("上传成功", res);

        if (res.statusCode != 200) {

          return wx.showModal({
            showCancel: false,
            content: res.errMsg || '上传失败',
          })
        }
        let resObj = JSON.parse(res.data);
        if (resObj.code) {

          return wx.showToast({
            title: resObj.msg,
            icon: 'none'
          })
        }
        file.status = "done";

        file.photo = resObj.data.path;

      },
      fail() {
        file.status = "failed";
      },
      complete() {
        that.setData({
          img_fileList
        })
      }
    });

  },
  video_afterRead(event){
    let that = this;
    const file = event.detail.file;
    let video_fileList = this.data.video_fileList;
    // console.log("file",file);
    video_fileList.push(file);
    file.status = "uploading";
    this.setData({
      video_fileList
    });
    wx.uploadFile({
      filePath: file.url,
      name: 'file',
      url: that.data.host + '/home/upload/video',
      success(res) {

        file.status = "failed";
        console.log("上传成功", res);

        if (res.statusCode != 200) {

          return wx.showModal({
            showCancel: false,
            content: res.errMsg || '上传失败',
          })
        }
        let resObj = JSON.parse(res.data);
        if (resObj.code) {

          return wx.showToast({
            title: resObj.msg,
            icon: 'none'
          })
        }
        file.status = "done";

        file.photo = resObj.data.path;

      },
      fail() {
        file.status = "failed";
      },
      complete() {
        that.setData({
          video_fileList
        })
      }
    });
  },
  delete_video(){
    this.setData({
      video_fileList:[]
    })
  },
  delete_image(e) {
    let {
      img_fileList = []
    } = this.data;
    img_fileList.splice(e.detail.index, 1);
    this.setData({
      img_fileList
    });

  },


  /**
   * 提交评价
   */
  submit_click() {
    // console.log(this.data.img_fileList);
    let pdc = this.data.pdc;
    let refund_num = parseFloat(this.data.refund_num.toString());
    if (!refund_num) {
      return wx.showToast({
        title: '请提交投诉数量',
        icon: 'error'
      })
    }
    if (pdc.product_number < refund_num) {
      return wx.showToast({
        title: '投诉数量不能超过购买数量',
        icon: 'none'
      })
    }
    let refund_amount = parseFloat(this.data.refund_amount.toString());
    console.log("refund_amount",pdc)
    if (pdc.refund_amount < refund_amount) {
      return wx.showToast({
        title: '索赔金额不能超过购买金额',
        icon: 'none'
      })
    }
    let photoArr = [];

    this.data.img_fileList.map(val => {

      photoArr.push(val.photo);

    });
	if (photoArr.length<=0) {
	  return wx.showToast({
	    title: '至少上传一张照片',
	    icon: 'none'
	  })
  }
  var videoArr=[]
  this.data.video_fileList.map(val=>{
    videoArr.push(val.photo);
  })
  if(videoArr.length<=0){
    return wx.showToast({
	    title: '请上传视频',
	    icon: 'none'
	  })
  }
    this.data.content= util.replaceStr(this.data.content)
    this.request({
      url: this.data.host + '/home/order/complaint',
      data: {
        refund_amount,
        refund_num,
        order_id: this.data.pdc.order_id,
        order_pid: this.data.pdc.order_product_id || this.data.pdc.yuyue_order_id,

        content: this.data.content,
        photos: photoArr.join(","),
        video:videoArr.toString()
      },
      method: "POST",
      loadingTip: '加密提交...',
     
    }, res => {
      wx.showModal({

        content: '提交成功！',
        showCancel: false,
        title: '恭喜',
        success: (result) => {
          wx.navigateBack({
            delta: 1,
          })
        },
      })
      //  console.log("提交评论",res)
    })
  }
})